package com.example.astronautcontact0123456789hwh1104;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class ContactDAL {
    private ContactSQLiteOpenHelper contactSQLiteOpenHelper;
    private android.content.Context context;
    private android.database.sqlite.SQLiteDatabase sqLiteDatabase;

    public ContactDAL(Context context) {
        this.context = context;
    }
    public void openDatabase(){
        contactSQLiteOpenHelper = new ContactSQLiteOpenHelper(context);
        try {
            sqLiteDatabase = contactSQLiteOpenHelper.getWritableDatabase();
        }catch (Exception e){
            sqLiteDatabase = contactSQLiteOpenHelper.getReadableDatabase();
        }
    }
    public void closeDatabase(){
        if(sqLiteDatabase != null){
            sqLiteDatabase.close();
            sqLiteDatabase = null;
        }
        if (contactSQLiteOpenHelper != null){
            contactSQLiteOpenHelper.close();
            contactSQLiteOpenHelper = null;
        }
    }
    public List<AstronautContact> queryAll(){
        List<AstronautContact> contactList=new ArrayList<>();
        try {
            openDatabase();
            Cursor cursor = sqLiteDatabase.query("contacts", null, null, null, null, null, null);
//        获得字段的索引号
            int idColumnIndex = cursor.getColumnIndex("id");
            int nameColumnIndex = cursor.getColumnIndex("name");
            int phoneColumnIndex = cursor.getColumnIndex("phone");
            int emailColumnIndex = cursor.getColumnIndex("email");
            int organizationColumnIndex = cursor.getColumnIndex("organization");
            int notesColumnIndex = cursor.getColumnIndex("notes");
//        根据索引号，读取字段内容，创建实体类对象，添加到List中
            if (cursor.moveToFirst()) {
                do{
                    int id = cursor.getInt(idColumnIndex);
                    String name = cursor.getString(nameColumnIndex);
                    String phone = cursor.getString(phoneColumnIndex);
                    String email = cursor.getString(emailColumnIndex);
                    String organization = cursor.getString(organizationColumnIndex);
                    String notes = cursor.getString(notesColumnIndex);
                    contactList.add(new AstronautContact(id, name, phone, email, organization, notes));
                }while (cursor.moveToNext()) ;
            }
            closeDatabase();
        }catch (Exception e){
            e.printStackTrace();
        }
        return contactList;
    }
    public long insertContact(AstronautContact contact){
        long result = -1;
        ContentValues values = new ContentValues();
        values.put("name", contact.getName());
        values.put("phone", contact.getPhone());
        values.put("email", contact.getEmail());
        values.put("organization", contact.getOrganization());
        values.put("notes", contact.getNotes());
        try {
            openDatabase();
            result = sqLiteDatabase.insert("contacts", null, values);
            closeDatabase();
        }catch (Exception e){
        }
        return result;
    }
    public AstronautContact queryById(int id){
        AstronautContact contact = null;
        try {
            openDatabase();
            Cursor cursor = sqLiteDatabase.query("contacts", null, "id=?", new String[]{String.valueOf(id)}, null, null, null);
            int nameColumnIndex = cursor.getColumnIndex("name");
            int phoneColumnIndex = cursor.getColumnIndex("phone");
            int emailColumnIndex = cursor.getColumnIndex("email");
            int organizationColumnIndex = cursor.getColumnIndex("organization");
            int notesColumnIndex = cursor.getColumnIndex("notes");
            if (cursor.moveToFirst()) {
                String name = cursor.getString(nameColumnIndex);
                String phone = cursor.getString(phoneColumnIndex);
                String email = cursor.getString(emailColumnIndex);
                String organization = cursor.getString(organizationColumnIndex);
                String notes = cursor.getString(notesColumnIndex);
                contact = new AstronautContact(id, name, phone, email, organization, notes);
            }
        }
        catch (Exception e){
        }
        return contact;
    }

    public long update(AstronautContact contact){
        long result = -1;
        ContentValues values = new ContentValues();
        values.put("name", contact.getName());
        values.put("phone", contact.getPhone());
        values.put("email", contact.getEmail());
        values.put("organization", contact.getOrganization());
        values.put("notes", contact.getNotes());
        try {
            openDatabase();
            result = sqLiteDatabase.update("contacts", values, "id=?", new String[]{String.valueOf(contact.getId())});
            closeDatabase();
        }
        catch (Exception e){
            e.printStackTrace();
            result=-1;
        }
        return result;
    }
    public long delete(int id){
        long result = -1;
        try {
            openDatabase();
            result = sqLiteDatabase.delete("contacts", "id=?", new String[]{String.valueOf(id)});
            closeDatabase();
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }
    public long deleteAll(){
        long result = -1;
        openDatabase();
        result = sqLiteDatabase.delete("contacts", null, null);
        closeDatabase();
        return result;
    }
    public void insertList(List<AstronautContact> contactList){
        //foreach
        for (AstronautContact contact : contactList) {
            insertContact(contact);
        }
    }
}
